package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.entity.Dict;
import com.atguigu.result.Result;
import com.atguigu.service.DictService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dict")
public class DictController extends BaseController {

    private static final String PAGE_INDEX = "dict/index";




    /**
     * 跳转'dict/index.html'页面
     *
     * @return
     */
    @RequestMapping
    public String index() {
        return PAGE_INDEX;
    }

    @Reference
    private DictService dictService;

    /**
     * 获取zTree节点数据
     *
     * @param id ：dict的parentId
     * @return
     */
    @ResponseBody
    @GetMapping("/findZnodes")
    public Result findZnodes(@RequestParam(defaultValue = "0") Long id) {
        List<Map<String, Object>> zNodes = dictService.findZnodesByParentId(id);
        return Result.ok(zNodes);
    }

    /**
     * 根据父节点的id获取子节点数据列表
     * @param parentId
     * @return
     */
    @GetMapping("/findDictListByParentId/{parentId}")
    @ResponseBody
    public Result findDictListByParentId(@PathVariable("parentId") Long parentId) {
        List<Dict> dictList = dictService.findDictListByParentId(parentId);
        return Result.ok(dictList);
    }

    /**
     * 根据父节点的dictCode获取子节点数据列表
     * @param dictCode
     * @return
     */
    @GetMapping("/findDictListByParentDictCode/{dictCode}")
    @ResponseBody
    public Result<List<Dict>> findDictListByParentDictCode(@PathVariable String dictCode) {
        List<Dict> list = dictService.findDictListByParentDictCode(dictCode);
        return Result.ok(list);
    }

    private final static String PAGE_CREATE = "community/create";
    @RequestMapping("/create")
    public String create(Model model){
        List<Dict> areaList = dictService.findDictListByParentDictCode("beijing");
        model.addAttribute("areaList",areaList);
        return PAGE_CREATE;
    }
}
